| Conditions | 1 |
| Paths | 16 |
| Total Lines | 186 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 29 | PAYONE.Service.CreditCardCheck = function (handler, form, config) { |
||
|
|
|||
| 30 | this.handler = handler; |
||
| 31 | this.form = form; |
||
| 32 | this.config = config; |
||
| 33 | this.origMethod = ''; |
||
| 34 | this.iframes = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Enhances payment.save and runs Validate and CreditCardCheck for CreditCards |
||
| 38 | * @todo rename this method? |
||
| 39 | * @param origMethod |
||
| 40 | */ |
||
| 41 | this.exec = function (origMethod) { |
||
| 42 | var check = this.handler.haveToValidate(); |
||
| 43 | |||
| 44 | if (check == 1) { |
||
| 45 | this.handler.origMethod = origMethod; |
||
| 46 | // Payone credit card payment method is available, and selected, initiate credit card check: |
||
| 47 | if (this.validate(this.form)) { |
||
| 48 | if(this.iframes == false) { |
||
| 49 | this.creditcardcheck(); |
||
| 50 | } else { |
||
| 51 | this.creditcardcheckHosted(); |
||
| 52 | } |
||
| 53 | } |
||
| 54 | } |
||
| 55 | else { |
||
| 56 | origMethod(); |
||
| 57 | } |
||
| 58 | }; |
||
| 59 | |||
| 60 | this.initHosted = function (fieldconfig, type_id) { |
||
| 61 | var configId = false; |
||
| 62 | var elementCcType = $('payone_creditcard_cc_type_select'); |
||
| 63 | if (elementCcType != undefined) { |
||
| 64 | var ccTypeConfigKey = elementCcType.value; |
||
| 65 | var ccTypeSplit = ccTypeConfigKey.split('_'); |
||
| 66 | configId = ccTypeSplit[0]; |
||
| 67 | var ccType = ccTypeSplit[1]; |
||
| 68 | $("payone_creditcard_cc_type").setValue(ccType); |
||
| 69 | } |
||
| 70 | |||
| 71 | aConfig = this.getConfig(); |
||
| 72 | request = aConfig.gateway[configId]; |
||
| 73 | |||
| 74 | var iframes = new Payone.ClientApi.HostedIFrames(fieldconfig, request); |
||
| 75 | iframes.setCardType(ccType); |
||
| 76 | |||
| 77 | document.getElementById(type_id).onchange = function () { |
||
| 78 | var elementCcType = $('payone_creditcard_cc_type_select'); |
||
| 79 | if (elementCcType != undefined) { |
||
| 80 | var ccTypeConfigKey = elementCcType.value; |
||
| 81 | var ccTypeSplit = ccTypeConfigKey.split('_'); |
||
| 82 | var ccType = ccTypeSplit[1]; |
||
| 83 | iframes.setCardType(ccType); // on change: set new type of credit card to process |
||
| 84 | } |
||
| 85 | }; |
||
| 86 | this.iframes = iframes; |
||
| 87 | return iframes; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Trigger CVC Code as configured |
||
| 92 | * |
||
| 93 | * @param element |
||
| 94 | */ |
||
| 95 | this.displayCheckCvc = function (element) { |
||
| 96 | config = $('payone_creditcard_config_cvc').value.evalJSON(); |
||
| 97 | ccKey = element.value; |
||
| 98 | var cvcDiv = $("payone_creditcard_cc_cid_div"); |
||
| 99 | if (cvcDiv != undefined && cvcDiv != null) { |
||
| 100 | configCcKey = config[ccKey]; |
||
| 101 | if (configCcKey != undefined && (configCcKey == 'always' || (configCcKey == 'only_first' && $('payone_cc_check_validation').value == 1))) { |
||
| 102 | cvcDiv.show(); |
||
| 103 | $('payone_cc_check_validation').value = 1; |
||
| 104 | } else { |
||
| 105 | cvcDiv.hide(); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | }; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Validate the Form Data |
||
| 112 | * |
||
| 113 | * @param form Form Object |
||
| 114 | * @return {*} |
||
| 115 | */ |
||
| 116 | this.validate = function (form) { |
||
| 117 | var elementCcType = $('payone_creditcard_cc_type_select'); |
||
| 118 | if (elementCcType != undefined) { |
||
| 119 | var ccTypeConfigKey = elementCcType.value; |
||
| 120 | var ccTypeSplit = ccTypeConfigKey.split('_'); |
||
| 121 | var configId = ccTypeSplit[0]; |
||
| 122 | |||
| 123 | var ccType = ccTypeSplit[1]; |
||
| 124 | $("payone_creditcard_config_id").setValue(configId); |
||
| 125 | $("payone_creditcard_cc_type").setValue(ccType); |
||
| 126 | } |
||
| 127 | |||
| 128 | config = this.getConfig(); |
||
| 129 | configValidation = config.validation; |
||
| 130 | |||
| 131 | validation = new PAYONE.Validation.CreditCard(configValidation); |
||
| 132 | return validation.validate(form); |
||
| 133 | }; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Perform creditcard check via Payone Client API |
||
| 137 | */ |
||
| 138 | this.creditcardcheck = function () { |
||
| 139 | var configId = $("payone_creditcard_config_id").value; |
||
| 140 | |||
| 141 | config = this.getConfig(); |
||
| 142 | configGateway = config.gateway[configId]; |
||
| 143 | |||
| 144 | var data = this.mapRequestCreditCardCheck(); |
||
| 145 | |||
| 146 | var payoneGateway = new PAYONE.Gateway( |
||
| 147 | configGateway, |
||
| 148 | function (response) { |
||
| 149 | return window.payone.handleResponseCreditcardCheck(response, false); |
||
| 150 | } |
||
| 151 | ); |
||
| 152 | payoneGateway.call(data); |
||
| 153 | }; |
||
| 154 | |||
| 155 | this.creditcardcheckHosted = function () { |
||
| 156 | if (this.iframes.isComplete()) { |
||
| 157 | $('payone_creditcard_hosted_error').hide(); |
||
| 158 | $('payone_creditcard_cc_owner').value = $('firstname').value + ' ' + $('lastname').value; |
||
| 159 | this.iframes.creditCardCheck('processPayoneResponseCCHosted'); |
||
| 160 | } else { |
||
| 161 | $('payone_creditcard_hosted_error').show(); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Collect PAYONE CreditCardCheck Request Parameters |
||
| 167 | * |
||
| 168 | * @return {*} |
||
| 169 | */ |
||
| 170 | this.mapRequestCreditCardCheck = function () { |
||
| 171 | data = { |
||
| 172 | 'cardexpiremonth':$('payone_creditcard_cc_expiration_month').value, |
||
| 173 | 'cardexpireyear':$('payone_creditcard_cc_expiration_year').value, |
||
| 174 | 'cardtype':$('payone_creditcard_cc_type').value |
||
| 175 | }; |
||
| 176 | if($('payone_pseudocardpan').value == '') { |
||
| 177 | data.cardholder = $('payone_creditcard_cc_owner').value; |
||
| 178 | data.cardpan = $('payone_creditcard_cc_number').value; |
||
| 179 | } else { |
||
| 180 | data.pseudocardpan = $('payone_pseudocardpan').value; |
||
| 181 | } |
||
| 182 | |||
| 183 | cid = $('payone_creditcard_cc_cid'); |
||
| 184 | if (cid != undefined) { |
||
| 185 | data.cardcvc2 = cid.value; |
||
| 186 | } |
||
| 187 | |||
| 188 | return data; |
||
| 189 | }; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Handle response |
||
| 193 | * |
||
| 194 | * @param response |
||
| 195 | * @return {Boolean} |
||
| 196 | */ |
||
| 197 | this.handleResponseCreditcardCheck = function (response, blIsHostedIframe) { |
||
| 198 | return this.handler.handleResponse(response, blIsHostedIframe); |
||
| 199 | }; |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get Config (auto-initialize) |
||
| 203 | * |
||
| 204 | * @return {*} |
||
| 205 | */ |
||
| 206 | this.getConfig = function () { |
||
| 207 | if (this.config == '' || this.config == undefined) { |
||
| 208 | configJson = $('payone_creditcard_config').value; |
||
| 209 | this.config = configJson.evalJSON(); |
||
| 210 | } |
||
| 211 | |||
| 212 | return this.config; |
||
| 213 | }; |
||
| 214 | }; |
||
| 215 | |||
| 464 | } |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.